package org.films.domain;

import java.security.SecureRandom;
import java.util.List;
import java.util.Random;
import org.films.domain.Film;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

privileged aspect FilmDataOnDemand_Roo_DataOnDemand {
    
    declare @type: FilmDataOnDemand: @Component;    
    
    private Random FilmDataOnDemand.rnd = new SecureRandom();    
    
    private List<Film> FilmDataOnDemand.data;    
    
    public Film FilmDataOnDemand.getNewTransientFilm(int index) {    
        org.films.domain.Film obj = new org.films.domain.Film();        
        obj.setDescription("description_" + index);        
        obj.setRating(new Double(index));        
        obj.setTitle("title_" + index);        
        obj.setTotalSum(null);        
        return obj;        
    }    
    
    public Film FilmDataOnDemand.getRandomFilm() {    
        init();        
        Film obj = data.get(rnd.nextInt(data.size()));        
        return Film.findFilm(obj.getId());        
    }    
    
    public boolean FilmDataOnDemand.modifyFilm(Film obj) {    
        return false;        
    }    
    
    @Transactional(propagation = Propagation.REQUIRES_NEW)    
    public void FilmDataOnDemand.init() {    
        if (data != null) {        
            return;            
        }        
                
        data = org.films.domain.Film.findFilmEntries(0, 10);        
        if (data == null) throw new IllegalStateException("Find entries implementation for 'Film' illegally returned null");        
        if (data.size() > 0) {        
            return;            
        }        
                
        data = new java.util.ArrayList<org.films.domain.Film>();        
        for (int i = 0; i < 10; i++) {        
            org.films.domain.Film obj = getNewTransientFilm(i);            
            obj.persist();            
            data.add(obj);            
        }        
    }    
    
}
